package com.mm.config;

import com.mm.service.CustomUserDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;
import org.springframework.security.oauth2.provider.token.DefaultTokenServices;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

import javax.sql.DataSource;

@Configuration
@EnableAuthorizationServer  //启动认证服务器的功能
public class OAuth2Configuration extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private DataSource dataSource;
    @Autowired
    private RedisConnectionFactory redisConnectionFactory;
    @Autowired
    private CustomUserDetailsService customUserDetailsService;
    @Autowired
    private AuthenticationManager authenticationManager;

    //client的信息的读取:JDBC的方式
    @Bean
    public JdbcClientDetailsService jdbcClientDetailsService() {
        return new JdbcClientDetailsService(dataSource);
    }

    //token存储方式:redis
    @Bean
    public RedisTokenStore redisTokenStore(){
        return new RedisTokenStore(redisConnectionFactory);
    }

    //自定义TokenServices的时候,需要设置@Primary,否则报错
    @Primary
    @Bean
    public DefaultTokenServices defaultTokenServices(){
        DefaultTokenServices tokenServices = new DefaultTokenServices();
        tokenServices.setTokenStore(redisTokenStore()); //token存储方式:redis存储
        tokenServices.setSupportRefreshToken(true); //支持刷新token
        tokenServices.setClientDetailsService(jdbcClientDetailsService());  //使用jdbc查数据库(主要是oauth_client_details表)的方式
        tokenServices.setAuthenticationManager(authenticationManager); //直接注入一个AuthenticationManager,自动开启密码授权类型
        tokenServices.setAccessTokenValiditySeconds(60 * 60 * 24); //access_token有效期,默认1天
        tokenServices.setRefreshTokenValiditySeconds(60 * 60 * 24 * 3);//refresh_token有效期,默认3天
        return tokenServices;
    }

    //client的信息的读取
    @Override
    public void configure(ClientDetailsServiceConfigurer clients)
            throws Exception {
        clients.withClientDetails(jdbcClientDetailsService());//使用jdbc查数据库(主要是oauth_client_details表)的方式
    }

    //声明安全约束,哪些允许访问,哪些不允许访问
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security)
            throws Exception {
        security.tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()")
                .allowFormAuthenticationForClients();//让/oauth/token支持client_id以及client_secret作登录认证
    }

    //声明授权和token的端点以及token的服务的一些配置信息,如采用什么存储方式和token的有效期等
    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints)
            throws Exception {
        endpoints
                .tokenStore(redisTokenStore()) //token存储方式:redis存储
                .userDetailsService(customUserDetailsService)   //使用自定义校验
                .authenticationManager(authenticationManager);  //直接注入一个AuthenticationManager,自动开启密码授权类型
        endpoints.tokenServices(defaultTokenServices());
    }

}